'use strict'

class write {
  constructor() {
    this.param = '/'
  }

  async post(c) {
    console.log(c.body);
    let {article_title,article_txt,article_time,user_id} = c.body
    let a = c.service.fun;
    //console.log(article_txt)
    //console.log(article_title)
    c.setHeader('content-type', 'text/html; charset=utf-8')


    let f = c.getFile('media');//获取到第一个上传的文件
    console.log('f：' + f)
      if(f === null){
        c.status(400);
        c.res.body = 'Bad request:not found media';
      }

    let filename = `${c.helper.makeName()}`+`${c.helper.extName(f.filename)}`;//就是生成了2020-12-3.jpg这个文件
    let fname = await f.toFile('./public/image/chuangzuoimg',filename)//把这个文件存在./images下
    console.log('filename'+filename);
    //console.log(r);
    //c.res.body = fname;

    a.add('article_list',{'article_title':article_title,'article_txt':article_txt,'article_img':`/static/image/chuangzuoimg/${filename}`,'article_time':article_time,'poster_id':user_id},c.service.pool);
    c.send({
      type:'success',
      data:'ok'
    })
  }
}

module.exports = write;

